package com.stgy.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.model.domain.LeaseTerm;
import com.stgy.lease.web.admin.service.LeaseTermService;
import com.stgy.lease.web.admin.mapper.LeaseTermMapper;
import org.springframework.stereotype.Service;

/**
 * @author lingx
 * @description 针对表【lease_term(租期)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:29
 */
@Service
public class LeaseTermServiceImpl extends ServiceImpl<LeaseTermMapper, LeaseTerm>
        implements LeaseTermService {

    ////更新或修改房间租期
    @Override
    public Boolean saveOrUpdateByStats(LeaseTerm leaseTerm) {

        //leaseTerm ==null
        //leaseTerm.monthCount == & leaseTerm.unit id!=
        if (leaseTerm == null || leaseTerm.getMonthCount() == null || leaseTerm.getUnit() == null) {
            throw new StgyException(ResultCodeEnum.TERM_EXISTS_ERROR);
        }
        LambdaQueryWrapper<LeaseTerm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaseTerm::getMonthCount, leaseTerm.getMonthCount());
        wrapper.eq(LeaseTerm::getUnit, leaseTerm.getUnit());
        wrapper.last("limit 1");
        LeaseTerm one = this.getOne(wrapper);
        if (one != null && !one.getId().equals(leaseTerm.getId())) {
            throw new StgyException(ResultCodeEnum.TERM_EXISTS_ERROR);
        }
        return this.saveOrUpdate(leaseTerm);
    }
}




